//
//  SubTaskDetailViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/8/17.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "SubTaskDetailViewController.h"


@interface SubTaskDetailViewController ()<UITextFieldDelegate,UITextViewDelegate>{
    
    NSDictionary *data; //子任务任务明细
    UILabel *title;
    
    UITapGestureRecognizer *tapGecognizer;
    UIScrollView *scrollView;
    UIView *container;
    UIView *under;
    UIView *under2;
    UIView *under3;
    UIView *under4;
    UIWebView *webView;
    
    UIButton *startButton;    //重新启动
    UIButton *stopButton;     //停止
    UIButton *compleButton;   //完成
    
    LeftTitleTextField *addTime;    //添加时间
    LeftTitleTextField *dExpectTime;    //任务时间
    LeftTitleTextField *dFinishType;    //完成状态
    LeftTitleTextField *dStatus;        //任务状态
    LeftTitleTextField *dUpdateTime;    //任务更新时间
    LeftTitleTextField *isExecute;      //是否需要当前用户执行
    
    //提交执行情况
    UILabel *perExplain;    //滑块说明
    UILabel *implementation;    //提交执行情况总标题
    UISlider *per;  //滑块
    UIButton *showExpectTextViewButton;
    UITextView *implementationDetails;  //执行情况内容
    UILabel *perValue;  //滑块的值
    UIButton *sendImplementationButton; //提交
    UIButton *clearButton;  //清空
    
    
    //执行情况
    NSDictionary *executedData;
    UILabel *impleMentAtionText;
    LeftTitleTextField *person;    //人员名称
    LeftTitleTextField *department;    //部门
    LeftTitleTextField *complteState;    //完成状态
    LeftTitleTextField *ExpectDetal;   //执行情况
    UITextView *expectTextView;
    LeftTitleTextField *endUpTime;  //最后更新时间
    LeftTitleTextField *LeadershipReview;   //领导批阅
    UITextView *leaShipTextView;    //领导批阅内容
    LeftTitleTextField *perLeftTitle;   //完成百分比
    LeftTitleTextField *isManager;  //是否为管理
    LeftTitleTextField *readState;    //查看状态
    LeftTitleTextField *readTime;   //查看时间
    
    BOOL isShowExpect;  //是否展开执行详情
    
}

@end

@implementation SubTaskDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    [self setUI];
}

- (void)viewDidDisappear:(BOOL)animated{
    
}

- (void)viewWillAppear:(BOOL)animated{
    
}

//获取子任务明细
- (void)getData{
    NSString *ashx = @"/Server/TaskHandler.ashx";
    NSDictionary *dic = @{@"act":@"getcd",@"cid":self.cid,@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        [GMDCircleLoader setOnView:scrollView withTitle:@"" animated:YES];
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"子任务明细:%@",model);
                data = model[@"data"][0];
                
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            NSLog(@"错误%@",error);
            [XHToast showBottomWithText:@"请求失败"];
        }
        [self getExecutedData];
    }];
    
}

//获取执行情况数据
- (void)getExecutedData{
    NSString *ashx = @"/Server/TaskHandler.ashx";
    
    NSDictionary *dic = @{@"act":@"getud",@"uid":self.uid,@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"子任务执行情况:%@",model);
                executedData = model[@"data"][0];
                [self setValue];
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            NSLog(@"错误%@",error);
            [XHToast showBottomWithText:@"请求失败"];
        }
    }];
    [GMDCircleLoader hideFromView:scrollView animated:YES];
}
//设置开始按钮隐藏
- (void)startButtonHidden:(BOOL)hidden{
    CGFloat height  = 30*FitSize;
    if (hidden) {
        [startButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [startButton setTitle:@"" forState:UIControlStateNormal];
        return;
    }
    [startButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
    [startButton setTitle:@"重启" forState:UIControlStateNormal];
    
}
//设置停止按钮隐藏
- (void)stopButtonHidden:(BOOL)hidden{
    CGFloat height  = 30*FitSize;
    if (hidden) {
        [stopButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [stopButton setTitle:@"" forState:UIControlStateNormal];
        return;
    }
    [stopButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
    [stopButton setTitle:@"停止" forState:UIControlStateNormal];
    
}
//设置完成按钮隐藏
- (void)compleButtonHidden:(BOOL)hidden{
    CGFloat height  = 30*FitSize;
    if (hidden) {
        [compleButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [compleButton setTitle:@"" forState:UIControlStateNormal];
        return;
    }
    [compleButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
    [compleButton setTitle:@"完成" forState:UIControlStateNormal];
}
//提交执行情况
- (void)expectViewHidden:(BOOL)hidden{
    if (hidden) {
        //提交总标题
        [implementation mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [implementation setHidden:YES];
        //under4上所有控件隐藏
        for (UIView *view in under4.subviews) {
            [view mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
            view.hidden = YES;
        }
        
        [under4 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }else{
        
        
    }
    
    
}

- (void)setValue{
    title.text = [NSString stringWithFormat:@"子任务:%@",data[@"DetailTitle"]];
    addTime.text = data[@"dAddTime"];
    dExpectTime.text = data[@"dExpectTime"];
    dUpdateTime.text = data[@"dUpdateTime"];
    switch ([data[@"dStatus"] intValue]) {
        case -1:{
            dStatus.text = @"已停止";
        }
            break;
        case 0:{
            dStatus.text = @"进行中";
        }
            break;
        case 1:{
            dStatus.text = @"完成";
        }
            break;
            
            
    }
    
    //主任完成 子任务完成 或者 既不不是管理者也不是执行者 下面三个按钮就都不显示
    if ([data[@"dStatus"] isEqualToString:@"1"] ||[_mainData[@"Status"] isEqualToString:@"1"]
        
        ||
        
        (![executedData[@"isMng"] isEqualToString:@"1"] && ![executedData[@"isExecute"] isEqualToString:@"1"] )) {
        [self startButtonHidden:YES];
        [self stopButtonHidden:YES];
        [self compleButtonHidden:YES];
    }else if([executedData[@"tIsMng"] isEqualToString:@"1"] ){
        //主任没完成 子任务没完成  是管理者 不是执行者
        switch ([data[@"dStatus"] intValue]) {
            case 0:{
                //进行中
                [self startButtonHidden:YES];
                [self stopButtonHidden:NO];
                [self compleButtonHidden:NO];
            }
                break;
                
            case -1:{
                //已停止
                [self startButtonHidden:NO];
                [self stopButtonHidden:YES];
                [self compleButtonHidden:YES];
                
            }
                break;
                
            default:{
                dStatus.text = @"状态未知(default)";
            }
                break;
        }
    }else{
        [self compleButtonHidden:NO];
        
    }
    
    
    //控制执行情况控件到显示 主任务完成 子任务完成 执行操作完成 不是执行者 就隐藏
    if ([executedData[@"WorkTag"] isEqualToString:@"1"] || [data[@"dStatus"] isEqualToString:@"1"] || [_mainData[@"Status"] isEqualToString:@"1"] || ![executedData[@"isExecute"] isEqualToString:@"1"]){
        [self expectViewHidden:YES];
    }
    
    
    if ([data[@"isExecute"] isEqualToString:@"0"]) {
        isExecute.text = @"不需要执行";
    }else{
        isExecute.text = @"需要执行";
    }
    
    if ([data[@"dFinishType"] isEqualToString:@"0"]) {
        dFinishType.text = @"未完成";
    }else{
        dFinishType.text = @"已完成";
    }
    
    //执行情况
    person.text = executedData[@"RealName"];    //执行人员
    department.text = executedData[@"DepName"]; //执行部门
    switch ([executedData[@"WorkTag"] intValue]) {  //任务状态
        case -1:{
            complteState.text = @"未完成";
        }
            break;
        case 1:{
            complteState.text = @"完成";
        }
            break;
        case 2:{
            complteState.text = @"已转交";
        }
            break;
            
        default:{
            complteState.text = @"未知状态";
        }
            break;
    }

    //将执行情况从utf－8专为string
    NSString* strAfterDecodeByUTF8AndURI = [executedData[@"WorkNotes"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *expectString = [strAfterDecodeByUTF8AndURI stringByReplacingOccurrencesOfString:@"&line&" withString:@"-----------------------"];
    expectTextView.text = expectString;   //执行情况
    
    if ([expectString isEqualToString:@""]) {
        ExpectDetal.text = @"";
        ExpectDetal.textColor = [UIColor redColor];
        [expectTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }else{
        ExpectDetal.text = @"";
    }
    
    
    
    endUpTime.text = executedData[@"UpdateTime"];   //最后更新时间
    
    leaShipTextView.text = executedData[@"Instruction"];    //领导批注
    if ([executedData[@"Instruction"] isEqualToString:@""]) {
        LeadershipReview.text = @"暂无";
        LeadershipReview.textColor = [UIColor redColor];
        [leaShipTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }else{
        [leaShipTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(leaShipTextView.contentSize.height);
        }];
    }
    
    
    
    perLeftTitle.text = [NSString stringWithFormat:@"%@%@",executedData[@"FinishPer"],@"%"];    //完成比例
    switch ([executedData[@"tViewStatus"] intValue]) {
        case 0:{
            readState.text = @"未看";
        }
            break;
        case 1:{
            readState.text = @"已看";
        }
            break;
            
        default:{
            readState.text = @"未知状态";
        }
            break;
    }
    
    if ([executedData[@"tViewTime"] isEqualToString:@""]) {
        readTime.text = @"暂无";
        readTime.textColor = [UIColor redColor];
    }else{
        readTime.text = executedData[@"tViewTime"];
    }
    
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:data[@"dNotes"]]]];
    //提交执行情况，赋个初始化值
    per.value = [executedData[@"FinishPer"] intValue]/100.0;
    
    
    
}

- (void)setUI{
    
    WS(ws);
    self.view.backgroundColor = [UIColor whiteColor];
    scrollView = [[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    scrollView.scrollsToTop = NO;
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(ws.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    scrollView.bounces = YES;
    scrollView.scrollEnabled = YES;
    scrollView.scrollsToTop = NO;
    
    container = [UIView new];
    [scrollView addSubview:container];
    container.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(scrollView);
        make.edges.equalTo(scrollView).with.insets(UIEdgeInsetsMake(0,0,0,0));
    }];
    
    UIColor *fontColor = [UIColor blackColor];
    CGFloat height  = 30*FitSize;
    CGFloat width = 250*FitSize;
    CGFloat offer = 10*FitSize;
    
    title = [[UILabel alloc]init];
    [container addSubview:title];
    title.textAlignment = NSTextAlignmentCenter;
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(offer);
        make.width.mas_equalTo(KSWidth);
        make.height.mas_equalTo(height);
    }];
    
    under = [[UIView alloc]init];
    under.backgroundColor = [UIColor whiteColor];
    [container addSubview:under];
    [under mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(title.mas_bottom).offset(offer);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
    }];
    under.layer.cornerRadius = 8;
    
    
    
    addTime = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"添加时间:"];
    [under addSubview:addTime];
    [addTime hideUnderLine];
    [addTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(offer);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(height);
    }];
    
    
    dExpectTime = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"任务执行时间:"];
    [under addSubview:dExpectTime];
    [dExpectTime hideUnderLine];
    [dExpectTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(addTime.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    
    dUpdateTime = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"任务更新时间:"];
    [dUpdateTime hideUnderLine];
    [under addSubview:dUpdateTime];
    [dUpdateTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dExpectTime.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    
    dStatus = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"任务状态:"];
    [dStatus hideUnderLine];
    [under addSubview:dStatus];
    dStatus.textColor = [UIColor redColor];
    [dStatus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dUpdateTime.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    
    isExecute = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"是否需要我执行:"];
    [under addSubview:isExecute];
    [isExecute hideUnderLine];
    [isExecute mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dStatus.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    
    [under mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(isExecute.mas_bottom).offset(5);
    }];
    
    UILabel *subTaskCont = [[UILabel alloc]init];
    subTaskCont.text = @"子任务内容";
    subTaskCont.font = [UIFont systemFontOfSize:16];
    subTaskCont.textColor = [UIColor redColor];
    subTaskCont.textAlignment = NSTextAlignmentCenter;
    [container addSubview:subTaskCont];
    [subTaskCont mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under.mas_bottom).offset(offer);
        make.centerX.equalTo(container);
        make.width.mas_equalTo(KSWidth);
    }];
    
    under2 = [[UIView alloc]init];
    [container addSubview:under2];
    under2.backgroundColor = [UIColor whiteColor];
    [under2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(subTaskCont.mas_bottom).offset(offer);
        make.left.equalTo(under);
        make.right.equalTo(under);
        make.height.mas_equalTo(150);
    }];
    under2.layer.cornerRadius = 8;
    
    webView = [[UIWebView alloc]init];
    webView.backgroundColor = [UIColor redColor];
    [under2 addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.equalTo(under2.mas_bottom).offset(5);
        
    }];
    
    UILabel *implementationValue = [[UILabel alloc]init];
    implementationValue.text = @"任务执行情况";
    implementationValue.textAlignment = NSTextAlignmentCenter;
    implementationValue.textColor = [UIColor redColor];
    [container addSubview:implementationValue];
    [implementationValue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under2.mas_bottom).offset(offer);
        make.left.equalTo(under);
        make.right.equalTo(under);
    }];
    
    under3 = [[UIView alloc]init];
    [container addSubview:under3];
    under3.backgroundColor = [UIColor whiteColor];
    [under3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(implementationValue.mas_bottom).offset(offer);
        make.left.equalTo(under);
        make.right.equalTo(under);
    }];
    under3.layer.cornerRadius = 8;
    
    person = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"执行人员:"];
    [under3 addSubview:person];
    [person hideUnderLine];
    [person mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    
    department = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"执行部门:"];
    [under3 addSubview:department];
    [department hideUnderLine];
    [department mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(person.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    
    complteState = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"完成状态:"];
    [under3 addSubview:complteState];
    [complteState hideUnderLine];
    [complteState mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(department.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    

    
    endUpTime = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"最后更新时间:"];
    [under3 addSubview:endUpTime];
    [endUpTime hideUnderLine];
    [endUpTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(complteState.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    
    LeadershipReview = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"领导批注:"];
    [LeadershipReview hideUnderLine];
    [under3 addSubview:LeadershipReview];
    [LeadershipReview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(endUpTime.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    //领导批注
    leaShipTextView = [[UITextView alloc]init];
    [under3 addSubview:leaShipTextView];
    leaShipTextView.layer.borderWidth = 1;
    leaShipTextView.editable = NO;
    leaShipTextView.selectable = NO;
    leaShipTextView.font = [UIFont systemFontOfSize:15];
    [leaShipTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(LeadershipReview.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    
    perLeftTitle = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"完成百分比:"];
    [perLeftTitle hideUnderLine];
    [under3 addSubview:perLeftTitle];
    [perLeftTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(leaShipTextView.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    
    readState = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"查看状态:"];
    [readState hideUnderLine];
    [under3 addSubview:readState];
    [readState mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(perLeftTitle.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    
    readTime = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"最后查看时间:"];
    [readTime hideUnderLine];
    [under3 addSubview:readTime];
    [readTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(readState.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
        make.height.mas_equalTo(height);
    }];
    
    ExpectDetal = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"执行详情:"];
    [ExpectDetal hideUnderLine];
    [under3 addSubview:ExpectDetal];
    [ExpectDetal mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(readTime.mas_bottom).offset(offer);
        make.left.equalTo(addTime);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(height);
    }];
    //执行详情
    expectTextView = [[UITextView alloc]init];
    [under3 addSubview:expectTextView];
    expectTextView.layer.borderWidth = 1;
    expectTextView.editable = NO;
    expectTextView.selectable = NO;
    expectTextView.font = [UIFont systemFontOfSize:15];
    [expectTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ExpectDetal.mas_bottom).offset(5);
        make.left.equalTo(addTime);
        make.right.equalTo(addTime);
    }];
    
    showExpectTextViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [showExpectTextViewButton addTarget:self action:@selector(showExpect:) forControlEvents:UIControlEventTouchUpInside];
    [showExpectTextViewButton setTitle:@"展开->" forState:UIControlStateNormal];
    [showExpectTextViewButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [under3 addSubview:showExpectTextViewButton];
    [showExpectTextViewButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(ExpectDetal);
        make.left.equalTo(ExpectDetal.mas_right).offset(10);
    }];

    
    [under3 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(expectTextView.mas_bottom).offset(offer);
    }];
    
    
    
    implementation = [[UILabel alloc]init];
    implementation.text = @"提交任务执行情况";
    implementation.textAlignment = NSTextAlignmentCenter;
    implementation.textColor = [UIColor redColor];
    [container addSubview:implementation];
    [implementation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under3.mas_bottom).offset(offer);
        make.left.equalTo(under);
        make.right.equalTo(under);
    }];
    
    under4 = [[UIView alloc]init];
    [container addSubview:under4];
    under4.backgroundColor = [UIColor whiteColor];
    [under4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(implementation.mas_bottom).offset(offer);
        make.left.equalTo(under);
        make.right.equalTo(under);
    }];
    under4.layer.cornerRadius = 8;
    
    perExplain = [[UILabel alloc]init];
    [under4 addSubview:perExplain];
    perExplain.textAlignment  = NSTextAlignmentCenter;
    perExplain.text = @"执行百分比";
    [perExplain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(offer);
        make.width.mas_equalTo(200);
        make.centerX.equalTo(under4.mas_centerX);
    }];
    
    per = [[UISlider alloc]init];
    [under4 addSubview:per];
    per.continuous = YES;
    [per addTarget:self action:@selector(slidrValue:) forControlEvents:UIControlEventValueChanged];
    per.tintColor = [UIColor greenColor];
    [per mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(perExplain.mas_bottom).offset(offer);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(15);
    }];
    perValue = [[UILabel alloc]init];
    [under4 addSubview:perValue];
    perValue.textAlignment = NSTextAlignmentCenter;
    [perValue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(per.mas_bottom).offset(5);
        make.centerX.equalTo(perValue);
        make.width.mas_equalTo(KSWidth);
    }];
    
    impleMentAtionText = [[UILabel alloc]init];
    impleMentAtionText.text = @"任务执行情况文本";
    impleMentAtionText.textAlignment = NSTextAlignmentCenter;
    [under4 addSubview:impleMentAtionText];
    [impleMentAtionText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(perValue.mas_bottom).offset(offer);
        make.width.mas_equalTo(200);
        make.centerX.equalTo(under4.mas_centerX);
        make.height.mas_equalTo(height);
    }];
    
    implementationDetails = [[UITextView alloc]init];
    [under4 addSubview:implementationDetails];
    implementationDetails.layer.borderWidth = 1;
    implementationDetails.layer.borderColor = [UIColor lightGrayColor].CGColor;
    implementationDetails.delegate = self;
    implementationDetails.font = [UIFont systemFontOfSize:15];
    implementationDetails.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [implementationDetails mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(impleMentAtionText.mas_bottom).offset(offer);
        make.left.equalTo(per);
        make.right.equalTo(per);
        make.height.mas_equalTo(100);
    }];
    
    //提交按钮
    sendImplementationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [sendImplementationButton addTarget:self action:@selector(sendImplementation) forControlEvents:UIControlEventTouchUpInside];
    [under4 addSubview:sendImplementationButton];
    [sendImplementationButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(implementationDetails.mas_bottom).offset(offer);
        make.size.mas_equalTo(CGSizeMake(120, 30));
        make.left.mas_equalTo(35);
    }];
    [sendImplementationButton setTitle:@"提交" forState:UIControlStateNormal];
    sendImplementationButton.layer.cornerRadius = 5;
    sendImplementationButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
    
    clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [clearButton addTarget:self action:@selector(clearImplementation) forControlEvents:UIControlEventTouchUpInside];
    [under4 addSubview:clearButton];
    [clearButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(implementationDetails.mas_bottom).offset(offer);
        make.size.mas_equalTo(CGSizeMake(120, 30));
        make.right.mas_equalTo(-35);
    }];
    [clearButton setTitle:@"清空" forState:UIControlStateNormal];
    clearButton.layer.cornerRadius = 5;
    clearButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:0.000 alpha:1.000];
    
    
    [under4 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(sendImplementationButton.mas_bottom).offset(offer);
    }];
    
    CGFloat buttonHeighit = 0;
    startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [startButton setTitle:@"重新启动" forState:UIControlStateNormal];
    startButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:0.000 alpha:1.000];
    [container addSubview:startButton];
    startButton.tag = 9200 + 0;
    [startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under4.mas_bottom).offset(20);
        make.height.mas_equalTo(buttonHeighit);
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-15);
    }];
    [startButton addTarget:self action:@selector(TaskOperation:) forControlEvents:UIControlEventTouchUpInside];
    
    stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [stopButton setTitle:@"停止" forState:UIControlStateNormal];
    stopButton.backgroundColor = [UIColor redColor];
    [container addSubview:stopButton];
    stopButton.tag = 9200 + 1;
    [stopButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(startButton.mas_bottom).offset(10);
        make.height.mas_equalTo(buttonHeighit);
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-15);
    }];
    [stopButton addTarget:self action:@selector(TaskOperation:) forControlEvents:UIControlEventTouchUpInside];
    
    compleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [compleButton setTitle:@"完成" forState:UIControlStateNormal];
    compleButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
    [container addSubview:compleButton];
    compleButton.tag = 9200 + 2;
    [compleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(stopButton.mas_bottom).offset(10);
        make.height.mas_equalTo(buttonHeighit);
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-15);
    }];
    [compleButton addTarget:self action:@selector(TaskOperation:) forControlEvents:UIControlEventTouchUpInside];
    
    [container mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(compleButton.mas_bottom).offset(30);
    }];
    
}

- (void)showExpect:(UIButton *)sender{
    if (isShowExpect) {
        [expectTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [showExpectTextViewButton setTitle:@"展开 ->" forState:UIControlStateNormal];
        isShowExpect = NO;
    }else{
        float newSizeH = 0;
        CGSize constraint = CGSizeMake(expectTextView.contentSize.width - 15, CGFLOAT_MAX);
        CGSize size = [expectTextView.text sizeWithFont: expectTextView.font
                                      constrainedToSize:constraint
                                          lineBreakMode:UILineBreakModeWordWrap];
        newSizeH = size.height + 16.0 ;
        
        [expectTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(newSizeH);
        }];
        [showExpectTextViewButton setTitle:@"收缩 -<" forState:UIControlStateNormal];
        isShowExpect  = YES;
    
    }
    
    
}

- (void)clearImplementation{
    implementationDetails.text = @"";
}

//  提交任务执行情况网络请求
- (void)sendImplementation{
   [self textViewEnd];
    NSString *ashx = @"/Server/TaskHandler.ashx";
    NSString *perValueString = [perValue.text substringToIndex:perValue.text.length-1];
    if ([perValueString isEqualToString:@""] || perValueString == nil) {
        [XHToast showTopWithText:@"进度不能为空"];
        return ;
    }
    
    if ([implementationDetails.text isEqualToString:@""] || implementationDetails.text == nil) {
        [XHToast showTopWithText:@"详情不能为空"];
        return ;
    }
    NSDictionary *dic = @{@"act":@"submitutask",@"uid":self.uid,@"per":perValueString,@"info":implementationDetails.text, @"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    NSLog(@"提交任务参数%@",dic);
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        [GMDCircleLoader setOnView:scrollView withTitle:@"" animated:YES];
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"提交执行情况:%@",model);
                [self getData];
                [XHToast showCenterWithText:@"提交任务执行情况成功"];
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            NSLog(@"错误%@",error);
            [XHToast showBottomWithText:@"请求失败"];
        }
        [GMDCircleLoader hideFromView:scrollView animated:YES];
    }];
}

//子任务操作
- (void)TaskOperation:(UIButton *)sender{
    
    NSString *apstatus;
    NSString *apstatus2;
    switch (sender.tag - 9200) {
        case 0:{
            apstatus = @"urestart";
            apstatus2 = @"重启成功";
        }
            break;
        case 1:{
            apstatus = @"ustop";
            apstatus2 = @"停止成功";
        }
            break;
        case 2:{
            apstatus = @"ufinish";
            apstatus2 = @"完成任务";
        }
            break;
    }
    
    NSString *ashx = @"/Server/TaskHandler.ashx";
    NSDictionary *dic = @{@"act":@"aputask",@"uid":self.uid,@"apstatus":apstatus, @"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    NSLog(@"提交任务参数%@",dic);
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        [GMDCircleLoader setOnView:scrollView withTitle:@"" animated:YES];
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"提交执行情况:%@",model);
                [self getData];
                [XHToast showCenterWithText:apstatus2];
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            NSLog(@"错误%@",error);
            [XHToast showBottomWithText:@"请求失败"];
        }
        [GMDCircleLoader hideFromView:scrollView animated:YES];
    }];
    
    
}

//文本开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView{
    
    [scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-222);
    }];
    [scrollView setContentOffset:CGPointMake(0, scrollView.contentSize.height)];
    tapGecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(textViewEnd)];
    [scrollView addGestureRecognizer:tapGecognizer];
    
    
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    [self textViewEnd];
    
}
//自定义方法 添加手势 键盘收回 滑动控件滑到最下面 去除手势
- (void)textViewEnd{
    [implementationDetails resignFirstResponder];
    [scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
    }];
    [scrollView removeGestureRecognizer:tapGecognizer];
    
    
    
}


- (void)slidrValue:(UISlider *)slider{
    NSInteger valueInt = slider.value/0.1;
    NSInteger valueint2 = slider.value*100;
    valueint2 = valueint2%10;
    if (valueint2 > 5) {
        valueInt +=1;
    }
    perValue.text = [NSString stringWithFormat:@"%ld0%@",valueInt,@"%"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [per resignFirstResponder];
    [implementationDetails resignFirstResponder];
    
}




@end
